package cn.kgc.controller;

import cn.kgc.bean.pojo.CDepartment;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.service.DepartmentManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DepartmentManagerController {
    @Autowired
    private DepartmentManagerService dms;

    //查
    @RequestMapping("/selectdepartment")
    public Result selectDepartment(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CDepartment> list = dms.selectAll();
        PageInfo<CDepartment> pageInfo = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",pageInfo);
    }

    //删
    @RequestMapping(value = "/deletedepartment/{id}" ,method = RequestMethod.POST)
    public Result deleteDepartment(@PathVariable long id){
        boolean flag = dms.deleteDepartmentById(id);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //增
    @RequestMapping(value = "/adddepartment" ,method = RequestMethod.POST)
    public Result addDepartment(@RequestBody CDepartment cDepartment){
        boolean flag = dms.addDepartment(cDepartment);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //改
    @RequestMapping(value = "/updatedepartment",method = RequestMethod.POST)
    public Result updateDepartment(@RequestBody CDepartment cDepartment){
        boolean flag = dms.updateDepartment(cDepartment);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //根据id查询部门详情
    @RequestMapping(value = "/selectdepartmentbyid/{id}", method = RequestMethod.POST)
    public Result selectdepartmentbyid(@PathVariable long id){
        CDepartment cDepartment = dms.selectAllById(id);
        return Result.success(Constants.CODE_200,"",cDepartment);
    }

    //模糊查询
    @RequestMapping(value = "/selectdepartmentByName")
    public Result selectdepartmentByName(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String name){
        PageHelper.startPage(pageNum,pageSize);
        List<CDepartment> list = dms.selectByName(name);
        PageInfo<CDepartment> info = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",info);
    }
}
